package com.fet.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBUtil {

	public  static Object[][] getData(String sql,int columnNum,
			ConnectionFactory connectionFactory) throws Exception{
		PreparedStatement ps =  null;
		ResultSet rs = null;
		Object[][] data = null;
		Connection conn = null;
		try{
System.out.println(sql);
System.out.println("----------------------------------------");
			conn = connectionFactory.getConnection();
			ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			rs = ps.executeQuery();
			rs.last();
			int num = rs.getRow();
			rs.beforeFirst();
			data = new Object[num][columnNum];
			int i = 0;
			while(rs.next())
			{
				for(int j = 0; j< columnNum ; j++){
					data[i][j] = rs.getObject(j + 1);
				}
				i ++;
			}
		}finally{
			if(rs != null)
				rs.close();
			if(ps != null)
				ps.close();
			connectionFactory.closeConnection(); 
		}
		return data;
	}
	
}
